package com.zhongwang.mms.module.buy.service;

import com.zhongwang.mms.gen.entity.BShoppingPlan;
import com.zhongwang.mms.gen.entity.BShoppingPlanChange;
import com.zhongwang.mms.gen.entity.BShoppingPlanChangeLog;
import com.zhongwang.mms.gen.mapper.BShoppingPlanChangeLogMapper;
import com.zhongwang.mms.gen.mapper.BShoppingPlanChangeMapper;
import com.zhongwang.mms.gen.mapper.BShoppingPlanMapper;
import com.zhongwang.mms.module.buy.dao.BuyPlanDao;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.ReportPlanChangeModel;
import com.zhongwang.mms.module.buy.model.ShoppingPlanModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ShiroUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.zhongwang.mms.util.BeanUtil.copy;

/**
 * @Description: 采购管理 -> 采购计划编辑 Service
 *
 * @Author: China.sgl
 * @Date: 2020/4/7 16:25
 */
@SuppressWarnings("ALL")
@Service
public class BuyPlanChangeService {

    @Autowired
    private LogService logService;
    @Autowired
    private BuyPlanDao buyPlanDao;
    @Autowired
    private ReportPlanDao reportPlanDao;
    @Autowired
    private BShoppingPlanMapper bShoppingPlanMapper;
    @Autowired
    private BShoppingPlanChangeMapper bShoppingPlanChangeMapper;
    @Autowired
    private BShoppingPlanChangeLogMapper bShoppingPlanChangeLogMapper;



    /**
     * 按条件查询可变更采购计划
     *
     * @param shoppingPlanModel 条件
     * @return list
     */
    public List<ShoppingPlanModel> findBuyPlanChangeListByQuery(ShoppingPlanModel shoppingPlanModel) {
        return buyPlanDao.findBuyPlanChangeListByQuery(shoppingPlanModel);
    }

    /**
     * 采购计划变更
     *
     * @param shoppingPlanModel 变更内容
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int changeBuyPlan(ShoppingPlanModel shoppingPlanModel) {
        BShoppingPlan srcPlan = bShoppingPlanMapper.selectByPrimaryKey(shoppingPlanModel.getSppId());
        BShoppingPlanChange change = genBaseChange(shoppingPlanModel, srcPlan);
        genChangeLog(shoppingPlanModel, srcPlan);
        // 变更物料生成两条变更记录用于导入OA，一条原物料名称，新数量为0，一条新名称新数量，变更标识为2（变更物料）
        if (!shoppingPlanModel.getSppMatDesc().equals(srcPlan.getSppMatDesc())) {

            BShoppingPlanChange change1 = new BShoppingPlanChange();
            copy(change, change1);
            change1.setSpcMatDesc(srcPlan.getSppMatDesc());
            change1.setSpcNewNum(0.0);
            change1.setSpcChangeFlag(2);
            bShoppingPlanChangeMapper.insert(change1);

            BShoppingPlanChange change2 = new BShoppingPlanChange();
            copy(change, change2);
            change2.setSpcMatDesc(shoppingPlanModel.getSppMatDesc());
            change2.setSpcNum(0.0);
            change2.setSpcNewNum(shoppingPlanModel.getSppNum());
            change2.setSpcChangeFlag(2);
            bShoppingPlanChangeMapper.insert(change2);

        } else {
            // 其它变更生成一条记录用于导入OA， 标识为1
            BShoppingPlanChange change1 = new BShoppingPlanChange();
            copy(change, change1);
            change1.setSpcChangeFlag(1);
            change1.setSpcMatDesc(srcPlan.getSppMatDesc());
            change1.setSpcNewNum(shoppingPlanModel.getSppNum());
            bShoppingPlanChangeMapper.insert(change1);
        }

        // 如果是合报的采购计划, 查询采购计划对应的提报计划的变更记录，
        // 如果有物料名称变更或者数量变为0， 则将对应提报计划的合报关系清除
        if (srcPlan.getSppIntegraFlag() != null && srcPlan.getSppIntegraFlag() == 1) {
            List<ReportPlanChangeModel> list = reportPlanDao.findChangeLogBySppId(shoppingPlanModel.getSppId());
            for (ReportPlanChangeModel reportPlanChangeModel : list) {
                if ((!reportPlanChangeModel.getRpcMatDesc().equals(reportPlanChangeModel.getRpcNewMatDesc()))
                        || reportPlanChangeModel.getRpcNewNum().doubleValue() == 0) {
                    reportPlanDao.updateStatusAndChangeMarkAndSppIdById(2, 0, null, reportPlanChangeModel.getRpcRppId());
                }
            }
            // 如果对应提报计划只剩一条， 则采购计划合报状态置0
            if (reportPlanDao.getCountBySppId(shoppingPlanModel.getSppId()) < 2) {
                buyPlanDao.updateIntegraFlagById(0, shoppingPlanModel.getSppId());
            }
        }

        logService.saveLog("采购计划变更", "采购计划变更");
        return buyPlanDao.updateBuyPlanByChange(shoppingPlanModel);
    }

    private void genChangeLog(ShoppingPlanModel shoppingPlanModel, BShoppingPlan srcPlan) {
        BShoppingPlanChangeLog log = new BShoppingPlanChangeLog();
        log.setSplMatCode(srcPlan.getSppMatCode());
        log.setSplMatDesc(srcPlan.getSppMatDesc());
        log.setSplNewMatCode(shoppingPlanModel.getMatCode());
        log.setSplNewMatDesc(shoppingPlanModel.getSppMatDesc());
        log.setSplBrand(srcPlan.getSppBrand());
        log.setSplNewBrand(shoppingPlanModel.getSppBrand());
        log.setSplBuyer(srcPlan.getSppBuyer());
        log.setSplNewBuyer(shoppingPlanModel.getSppBuyer());
        log.setSplCategory(srcPlan.getSppCategory());
        log.setSplContractEndDate(srcPlan.getSppContractEndDate());
        log.setSplContractStartDate(srcPlan.getSppContractStartDate());
        log.setSplCreateTime(new Date());
        log.setSplCreateUser(ShiroUtils.getLoginName());
        log.setSplDesigner(srcPlan.getSppDesigner());
        log.setSplNum(srcPlan.getSppNum());
        log.setSplNewNum(shoppingPlanModel.getSppNum());
        log.setSplNewPlanner(shoppingPlanModel.getRppPlanner());
        log.setSplNewProjectCode(shoppingPlanModel.getRppProjectCode());
        log.setSplNewProjectName(shoppingPlanModel.getSppProjectName());
        log.setSplNewRemark(shoppingPlanModel.getOdaRemark());
        log.setSplNewSapCode(shoppingPlanModel.getSppSapCode());
        log.setSplNewTrackCode(shoppingPlanModel.getSppTrackCode());
        log.setSplNewUnit(shoppingPlanModel.getRppUnit());
        log.setSplPart(srcPlan.getSppPart());
        log.setSplUnit(srcPlan.getSppUnit());
        log.setSplTypeName(srcPlan.getSppTypeName());
        log.setSplTypeId(srcPlan.getSppTypeId());
        log.setSplTrackCode(srcPlan.getSppTrackCode());
        log.setSplTenderStartDate(srcPlan.getSppTenderStartDate());
        log.setSplTenderEndDate(srcPlan.getSppTenderEndDate());
        log.setSplTel(srcPlan.getSppTel());
        log.setSplSupplier(srcPlan.getSppSupplier());
        log.setSplStatus(1);
        log.setSplStandardNo(srcPlan.getSppStandardNo());
        log.setSplSppId(srcPlan.getSppId());
        log.setSplSapCode(srcPlan.getSppSapCode());
        log.setSplRowNo(srcPlan.getSppRowNo());
        log.setSplRemark(srcPlan.getSppRemark());
        log.setSplReason(shoppingPlanModel.getSpcReason());
        log.setSplQuoteStartDate(srcPlan.getSppQuoteStartDate());
        log.setSplQuoteEndDate(srcPlan.getSppQuoteEndDate());
        log.setSplProjectName(srcPlan.getSppProjectName());
        log.setSplProjectCode(srcPlan.getSppProjectCode());
        log.setSplProgress(srcPlan.getSppProgress());
        log.setSplPredictDate(srcPlan.getSppPredictDate());
        log.setSplPlanner(srcPlan.getSppPlanner());
        log.setSplPlanDate(srcPlan.getSppPlanDate());
        log.setSplPayCycle(srcPlan.getSppPayCycle());
        bShoppingPlanChangeLogMapper.insert(log);
    }

    /**
     * 生成采购计划变更基础信息
     *
     * @param target 变更计划
     * @param srcPlan 源计划
     * @return 采购计划变更对象
     */
    @NotNull
    private BShoppingPlanChange genBaseChange(ShoppingPlanModel target, BShoppingPlan srcPlan) {
        BShoppingPlanChange change = new BShoppingPlanChange();
        change.setSpcBrand(srcPlan.getSppBrand());
        change.setSpcBuyer(srcPlan.getSppBuyer());
        change.setSpcCategory(srcPlan.getSppCategory());
        change.setSpcChangeTime(new Date());
        change.setSpcDesigner(srcPlan.getSppDesigner());
        change.setSpcImportStatus(1);
        change.setSpcLocation(srcPlan.getSppWhiId().toString());
        change.setSpcUnit(srcPlan.getSppUnit());
        change.setSpcTrackNo(target.getSppTrackCode());
        change.setSpcTel(srcPlan.getSppTel());
        change.setSpcStandardNo(srcPlan.getSppStandardNo());
        change.setSpcSppId(srcPlan.getSppId());
        change.setSpcRemark(srcPlan.getSppRemark());
        change.setSpcProjectName(srcPlan.getSppProjectName());
        change.setSpcProjectCode(srcPlan.getSppProjectCode());
        change.setSpcPayCycle(srcPlan.getSppPayCycle());
        change.setSpcPart(srcPlan.getSppPart());
        change.setSpcNum(srcPlan.getSppNum());
        change.setSpcMatDesc(target.getSppSapCode());
        change.setSpcMatCode(srcPlan.getSppSapCode());
        change.setSpcReason(target.getSpcReason());
        change.setSpcDel(target.getSpcDel());
        change.setSpcPlanDate(srcPlan.getSppVerifyDate());
        change.setSpcNewBrand(target.getSppBrand());
        change.setSpcNewMatDesc(target.getSppMatDesc());
        change.setSpcNewProjectName(target.getSppProjectName());
        return change;
    }

    /**
     * 根据采购计划ID查询相关提报计划包含变更信息
     *
     * @param id 采购计划ID
     * @return list
     */
    public List<ReportPlanChangeModel> findReportPlanChangeList(Integer id) {
        return reportPlanDao.findPlanAndChangeLogBySppId(id);
    }

    /**
     * 查询采购计划相应的提报计划变更是否有变更物料描述的
     *
     * @param shoppingPlanModel
     * @return
     */
    public boolean selectStatus(ShoppingPlanModel shoppingPlanModel) {
        List<ReportPlanChangeModel> list = reportPlanDao.findChangeLogBySppId(shoppingPlanModel.getSppId());
        for (ReportPlanChangeModel reportPlanChangeModel : list) {
            if (!reportPlanChangeModel.getRpcMatDesc().equals(reportPlanChangeModel.getRpcNewMatDesc())) {
                return true;
            }
        }
        return false;
    }
}
